package com.class5.springboot.frame.context.bean;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.config.ResultEnum;
import com.class5.springboot.frame.web.result.ResultFailure;

import com.class5.springboot.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Map;

/**
 * 全局错误处理
 *
 * @author Administrator
 */

@Controller
public class SysErrorController extends SuperErrorController {

	// 是否在调试模式，调试模式下将直接输出验证码
	@Value("${myconfig.security.debugModel}")
	private String debugModel;
//	@Value("${myconfig.web.host}")
//	private String host;

	private boolean isDebugModel() {
		return "true".equals(debugModel);
	}

	@RequestMapping("/400")
	public void badRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map<String, String> map = getErrorMessage(request);
		ResultFailure rf = new ResultFailure(null, isDebugModel(), request);
		rf.setErrcode(map.get("status"));
		rf.setErrmsg(map.get("message"));
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().println(JSON.toJSONString(rf));
	}

	@RequestMapping("/404")
	public void notFound(HttpServletRequest request, HttpServletResponse response)throws IOException {
		// 考虑到vue刷新的问题，对404作特殊处理
/*		Map<String, String> msg = getErrorMessage(request);
		String path = msg.get("path");
		String contextPath = request.getContextPath();
		if (!path.startsWith(contextPath + "/pub") && !path.startsWith(contextPath + "/api") && !path.endsWith("index.html")) {
			response.sendRedirect(host + contextPath + "/index.html");
			return;
		}
*/
		response.setStatus(404);
		Map<String, String> map = getErrorMessage(request);
		ResultFailure rf = new ResultFailure(null, isDebugModel(), request);
		rf.setErrcode(map.get("status"));
		rf.setErrmsg(map.get("message"));
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().println(JSON.toJSONString(rf));
	}

	@RequestMapping("/500")
	public void serverError(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException{
		Map<String, String> map = getErrorMessage(request);
		ResultFailure rf = new ResultFailure(e, isDebugModel(), request);
		rf.setErrcode(map.get("status"));
		if(map.get("message").equals(ResultEnum.LOGIN_ERROR_401)||
				map.get("message").equals(ResultEnum.LOGIN_ERROR_TOKENERROR)||
				map.get("message").equals(ResultEnum.LOGIN_ERROR_TOKEN_TIMEOUT)){
			rf.setErrcode("401");
			rf.setErrmsg(map.get("message"));
		}if(map.get("message").equals(ResultEnum.LOGIN_ERROR_403)){
			rf.setErrcode("403");
			rf.setErrmsg(map.get("message"));
		}
		JSONObject debug = rf.getDebug();
		if (debug!=null) {
			debug.put("webexp", map.get("trace").replace("\t", "    ").replace("\r","").split("\n"));
		}else{
			if(StringUtils.isNull(rf.getErrmsg())){
				rf.setErrmsg(map.get("message"));
			}
		}
		if(StringUtils.isNull(rf.getErrmsg())){
			rf.setErrmsg("系统错误");
		}
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().println(JSON.toJSONString(rf));
	}
}
